package com.example.pufang.controller.front;

import com.example.common.CommonResult;
import com.example.common.PageParamRequest;
import com.example.exception.ExceptionCodeEnum;
import com.example.pufang.pojo.Collect;
import com.example.pufang.pojo.House;
import com.example.pufang.pojo.HouseViewLog;
import com.example.pufang.pojo.User;
import com.example.pufang.service.CollectService;
import com.example.pufang.service.HouseService;
import com.example.pufang.service.HouseViewLogService;
import com.example.pufang.service.UserService;
import com.example.pufang.vo.HouseFrontDetailVo;
import com.example.pufang.vo.HouseListVo;
import com.example.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/house")
@CrossOrigin
@Api(tags = "h5房屋接口")
public class HouseFrontController {

    @Autowired
    private UserService userService;

    @Autowired
    private HouseService houseService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private TokenUtil tokenUtil;

    @Operation(summary = "发布房屋信息")
    @PostMapping("/publish")
    public CommonResult<Boolean> publish(@RequestBody House house, HttpServletRequest request) {

        // 获取当前用户
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        house.setUserId(user.getId());

        return CommonResult.success(houseService.publish(house));

    }

    @Operation(summary = "获取房源分页列表")
    @GetMapping("/list")
    public CommonResult<List<HouseListVo>> getHouseList(PageParamRequest pageParamRequest, String searchText, String district,
                                                        Integer room, Integer parlor, Integer lavatory, String order) {

        return CommonResult.success(houseService.getHouseList(pageParamRequest, searchText, district, room, parlor, lavatory, order));
    }

    @Operation(summary = "获取当前用户所有的收藏列表")
    @GetMapping("/collectList")
    public CommonResult<List<HouseListVo>> getHouseCollectList(PageParamRequest pageParamRequest, HttpServletRequest request) {

        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        return CommonResult.success(houseService.getHouseCollectList(pageParamRequest, user.getId()));
    }

    @Operation(summary = "获取当前用户浏览历史列表")
    @GetMapping("/viewHistoryList")
    public CommonResult<List<HouseListVo>> getHouseViewHistoryList(PageParamRequest pageParamRequest, HttpServletRequest request) {

        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        return CommonResult.success(houseService.getHouseViewHistoryList(pageParamRequest, user.getId()));
    }

    @Operation(summary = "我的房源列表")
    @GetMapping("/myHouseList")
    public CommonResult<List<HouseListVo>> getMyHouseList(PageParamRequest pageParamRequest, HttpServletRequest request) {

        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        return CommonResult.success(houseService.getHouseListByUserId(pageParamRequest, user.getId()));
    }

    @Operation(summary = "通过id获取房源信息")
    @GetMapping("/detail")
    public CommonResult<HouseFrontDetailVo> getHouseDetail(String id, HttpServletRequest request) {
        // 获取浏览用户
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        return CommonResult.success(houseService.getHouseDetail(id, user));
    }

    @Operation(summary = "收藏当前房源")
    @PostMapping("/collect")
    public CommonResult<Boolean> collectHouse(@RequestBody Collect collect, HttpServletRequest request) {

        collect.setUserId(userService.getUserByPhone(tokenUtil.getFrontUserPhone(request)).getId());

        return CommonResult.success(collectService.save(collect));
    }

    @Operation(summary = "取消收藏当前房源")
    @PostMapping("/cancelCollect")
    public CommonResult<Boolean> cancelCollectHouse(@RequestBody Collect collect, HttpServletRequest request) {

        collect.setUserId(userService.getUserByPhone(tokenUtil.getFrontUserPhone(request)).getId());

        return CommonResult.success(collectService.deleteByHouseUser(collect));
    }

    @Operation(summary = "删除房源")
    @PostMapping("/delete")
    public CommonResult<Boolean> delete(@RequestBody String id, HttpServletRequest request) {
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        House house = houseService.getById(id);

        if (house.getUserId() != user.getId()) {
            return CommonResult.failed("没有当前房源修改权限");
        }

        house.setIsDelete(true)
                .setDeleteTime(new Date());

        if (houseService.updateById(house)) {
            return CommonResult.success("删除成功");
        } else {
            return CommonResult.failed("删除失败");
        }
    }

    @Operation(summary = "公开房源")
    @PostMapping("/display")
    public CommonResult<Boolean> display(@RequestBody String id, HttpServletRequest request) {
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        House house = houseService.getById(id);

        if (house.getUserId() != user.getId()) {
            return CommonResult.failed("没有当前房源修改权限");
        }

        house.setIsDisplay(true);

        if (houseService.updateById(house)) {
            return CommonResult.success("更新成功");
        } else {
            return CommonResult.failed("更新失败");
        }
    }

    @Operation(summary = "取消公开房源")
    @PostMapping("/cancelDisplay")
    public CommonResult<Boolean> cancelDisplay(@RequestBody String id, HttpServletRequest request) {
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        House house = houseService.getById(id);

        if (house.getUserId() != user.getId()) {
            return CommonResult.failed("没有当前房源修改权限");
        }

        house.setIsDisplay(false);

        if (houseService.updateById(house)) {
            return CommonResult.success("更新成功");
        } else {
            return CommonResult.failed("更新失败");
        }
    }

    @Operation(summary = "保存修改")
    @PostMapping("/saveChange")
    public CommonResult<Boolean> saveChange(@RequestBody House house, HttpServletRequest request) {
        User user = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));

        if (user.getId() != house.getUserId()) {
            return CommonResult.failed("没有当前房源修改权限");
        }

        return CommonResult.success(houseService.updateById(house), "修改成功");
    }

    @Operation(summary = "通过房屋id获取房东")
    @GetMapping("/getUserByHouseId")
    public CommonResult<Map<String, Object>> getUserByHouseId(String id, HttpServletRequest request) {

        HashMap<String, Object> map = new HashMap<>();

        House house = houseService.getById(id);
        User receiver = userService.getById(house.getUserId());

        User sender = userService.getUserByPhone(tokenUtil.getFrontUserPhone(request));
        if (sender.getId() == receiver.getId()) {
            return CommonResult.failed(ExceptionCodeEnum.CHAT_SELF, "不能和自己聊天");
        }

        map.put("receiverId", receiver.getId());
        map.put("receiverName", receiver.getUsername());

        return CommonResult.success(map);


    }

}
